# 爬取高德地图天气

# 获取城市列表
import json

import requests


class WeatherUtil(object):

    def __init__(self, city_url, weather_url) -> None:
        self.city_url = city_url
        self.weather_url = weather_url

    # 获取全国城市不包括区,最细维度到地级市
    def get_city_list(self):
        payload = {}
        headers = {}
        response = requests.request("GET", self.city_url, headers=headers, data=payload)
        response.encoding = response.apparent_encoding
        if not response.text:
            return {}
        response_json = json.loads(response.text)
        if response_json['status'] != '1':
            return {}
        city_data = response_json['data']['cityData']
        cityByLetter = response_json['data']['cityByLetter']
        other_city = city_data['otherCitys']

        city_list = []
        for letter in cityByLetter:
            temp_list = cityByLetter.get(letter)
            for temp in temp_list:
                city_list.append(temp)
        for other in other_city:
            city_list.append(other)
        city_ret = {}
        for city in city_list:
            city_ret.setdefault(city['name'], city)
        return city_ret

    def get_weather(self, city_code):
        weather_url = self.weather_url % city_code
        payload = {}
        headers = {}
        response = requests.request("GET", weather_url, headers=headers, data=payload)
        if not response.text:
            return {}
        response_json = json.loads(response.text)
        if response_json['status'] != '1':
            return {}
        weather_data = response_json['data']['data']
        weather_list = []
        for weather in weather_data:
            weather_ret = {}
            weather_ret.setdefault('时间', weather.get('forecast_date'))
            forecast_data = weather.get('forecast_data')
            weather_detail_list = []
            for data in forecast_data:
                weather_detail = {}
                desc = '白天' if data.get('daynight') == 0 else '夜间'
                weather_detail.setdefault('时间段', desc)
                weather_detail.setdefault('风向', data.get('wind_direction_desc'))
                weather_detail.setdefault('温度', data.get('min_temp') + "~" + data.get('max_temp') + "℃")
                weather_detail.setdefault('天气', data.get('weather_name'))
                weather_detail_list.append(weather_detail)
            weather_ret.setdefault('详情', weather_detail_list)
            weather_list.append(weather_ret)
        return weather_list

    def query_weather(self, keyword):
        if not keyword:
            raise Exception("请输入城市名称")
        # 查询城市code
        city_code = self.get_city_list()[keyword]['adcode']
        if not city_code:
            raise Exception("未查询到城市信息")
        if city_code:
            return self.get_weather(city_code)
        return []


if __name__ == '__main__':
    city_url = 'https://www.amap.com/service/cityList?version=2020112018'
    url = "https://www.amap.com/service/weather?adcode=%s"
    util = WeatherUtil(city_url, url)
    print(util.query_weather('黄石'))
